<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Solr7从搭建到入坑 - (power up)</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="Kevin Jiang" /><meta name="description" content="solr7搭建运行非常简单，首先下载压缩包，然后解压，到解压出来的目录执行：bin/solr start即可启动solr。以这种方式启动的so" /><meta name="keywords" content="KevinJiang, AI大模型, AI落地, 全栈工程师, Java, Spring Boot" />






<meta name="generator" content="Hugo 0.84.4 with theme even" />


<link rel="canonical" href="http://kevinjiang.info/post/searchengine/solr7%E4%BB%8E%E6%90%AD%E5%BB%BA%E5%88%B0%E5%85%A5%E5%9D%91/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">



<link href="/sass/main.min.78f8f17bab244b9ee62ad16480c9584d5fc2db06ae20681d1ca225cefd80767c.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="Solr7从搭建到入坑" />
<meta property="og:description" content="solr7搭建运行非常简单，首先下载压缩包，然后解压，到解压出来的目录执行：bin/solr start即可启动solr。以这种方式启动的so" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://kevinjiang.info/post/searchengine/solr7%E4%BB%8E%E6%90%AD%E5%BB%BA%E5%88%B0%E5%85%A5%E5%9D%91/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2017-10-08T14:10:26+00:00" />
<meta property="article:modified_time" content="2017-10-08T14:10:26+00:00" />

<meta itemprop="name" content="Solr7从搭建到入坑">
<meta itemprop="description" content="solr7搭建运行非常简单，首先下载压缩包，然后解压，到解压出来的目录执行：bin/solr start即可启动solr。以这种方式启动的so"><meta itemprop="datePublished" content="2017-10-08T14:10:26+00:00" />
<meta itemprop="dateModified" content="2017-10-08T14:10:26+00:00" />
<meta itemprop="wordCount" content="2224">
<meta itemprop="keywords" content="Solr," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Solr7从搭建到入坑"/>
<meta name="twitter:description" content="solr7搭建运行非常简单，首先下载压缩包，然后解压，到解压出来的目录执行：bin/solr start即可启动solr。以这种方式启动的so"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">(Power up)</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">归档</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/resume/">
        <li class="mobile-menu-item">简历</li>
      </a>
  </ul>

  


</nav>

  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">(Power up)</a>
</div>





<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">归档</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/resume/">简历</a>
      </li>
  </ul>
</nav>

    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">Solr7从搭建到入坑</h1>

      <div class="post-meta">
        <span class="post-time"> 2017-10-08 </span>
        
          <span class="more-meta"> 约 2224 字 </span>
          <span class="more-meta"> 预计阅读 5 分钟 </span>
        
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#以solrcloud模式启动solr">以SolrCloud模式启动Solr</a></li>
    <li><a href="#索引techproducts样例数据">索引techproducts样例数据</a>
      <ul>
        <li></li>
      </ul>
    </li>
    <li><a href="#基本查询">基本查询</a>
      <ul>
        <li><a href="#单个词查询">单个词查询</a></li>
        <li><a href="#字段查询">字段查询</a></li>
        <li><a href="#短语查询">短语查询</a></li>
        <li><a href="#组合搜索">组合搜索</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <p>solr7搭建运行非常简单，首先下载压缩包，然后解压，到解压出来的目录执行：<code>bin/solr start</code>即可启动solr。以这种方式启动的solr是single core的模式。推荐部署cloud模式，可以有更好的伸缩性与扩展性，使用：<code>bin/solr start -c</code>可以启动cloud模式。下面主要针对cloud模式介绍solr7.2.1的搭建与查询。</p>
<!-- raw HTML omitted -->
<p>solr7自带了几个例子，可以从例子可以深入solr。</p>
<h2 id="以solrcloud模式启动solr">以SolrCloud模式启动Solr</h2>
<p>要启动solr，在Linux或者Unix执行：<code>bin/solr start -e cloud</code>，windows下执行：<code>bin\solr.cmd start -e cloud</code>，默认启动2个node组成的cloud server。这将会进入一个交互模式的启动过程，如果想跳过交互模式，启动命令添加参数<code>-noprompt</code>。如果是交互模式，一路回车到下面的提示：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">Now let&#39;s create a new collection for indexing documents in your 2-node cluster.
Please provide a name for your new collection: [gettingstarted]
</code></pre></td></tr></table>
</div>
</div><p>输入<code>techproducts</code>回车，我们创建的collection名字是<code>techproducts</code>。</p>
<p>继续回车到选择配置的地方：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">Please choose a configuration for the techproducts collection, available options are:
_default or sample_techproducts_configs [_default]
</code></pre></td></tr></table>
</div>
</div><p>这里有一个configSet的概念，configSet相当于是一个所有配置文件存放的地方，所有创建的collection都要在这里读取配置文件。
<code>_default</code>是一个基本的配置集，还有一个包含了<code>techproducts</code>的配置集，这个配置集可以用来支持我们想使用的techproducts样例数据。
输入<code>sample_techproducts_configs</code>回车。</p>
<p>恭喜，Solr已经启动好了！通过在浏览器访问<code>http://localhost:8983/solr</code>，这将是入坑的起点。
通过上面的步骤，创建了了两个节点（2 nodes），一个运行在7574端口，另外一个运行在8983端口，一个collection <code>techproducts</code>，两个shard（分片），每个分片又有两个拷贝，这样的配置可以在某个分片出现问题的时候快速恢复。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">                            |-- 127.0.1.1:7574
               |-- shard1 --|-- 127.0.0.1:8983
techproducts --|
               |-- shard2 --|-- 127.0.1.1:7574
                            |-- 127.0.0.1:8983
</code></pre></td></tr></table>
</div>
</div><h2 id="索引techproducts样例数据">索引techproducts样例数据</h2>
<p>上面已经启动了solr，但是里面还没有数据，现在还不能做查询。</p>
<p>solr包含了一个可以提交多种数据格式的工具<code>bin/post</code>，下面使用这个脚本来提交样例数据。</p>
<blockquote>
<p>目前<code>bin/post</code>还不兼容Windows，在Windows系统需要另外的办法提交数据</p>
</blockquote>
<p>数据存放在<code>example/exampledocs</code>目录，这里面的数据格式包括JSON, CSV等等，这些格式的数据都可以一次性索引到solr里面。</p>
<h4 id="linuxmac">Linux/Mac</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-shell" data-lang="shell">bin/post -c techproducts example/exampledocs/*
</code></pre></td></tr></table>
</div>
</div><h4 id="windows">Windows</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-shell" data-lang="shell">java -jar -Dc<span class="o">=</span>techproducts -Dauto example<span class="se">\e</span>xampledocs<span class="se">\p</span>ost.jar example<span class="se">\e</span>xampledocs<span class="se">\*</span>
</code></pre></td></tr></table>
</div>
</div><p>恭喜！索引完数据就可以做查询了。</p>
<h2 id="基本查询">基本查询</h2>
<p>Solr提供了基于http的REST接口以供多种方式进行查询。</p>
<p>浏览器访问<code>http://localhost:8983/solr/#/techproducts/query</code>界面，直接点击<code>Execut Query</code>按钮，右边将会显示10条以JSON格式返回的数据。在查询表单可以有很多的参数可以填写，最基本的就是<code>q</code>参数，需要查询的内容都可以通过<code>q</code>字段指定。还有很多其他的参数以后用到再一一介绍。</p>
<h3 id="单个词查询">单个词查询</h3>
<p>单个词的意思是不能再进行分词的某个词，指定<code>q</code>等于这个词即可，浏览器访问下面的url：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">http://localhost:8983/solr/techproducts/select?q=foundation
</code></pre></td></tr></table>
</div>
</div><p>可以得到下面的结果（实际有4条数据，下面截取了第一条数据）：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-json" data-lang="json"><span class="p">{</span>
  <span class="nt">&#34;responseHeader&#34;</span><span class="p">:{</span>
    <span class="nt">&#34;zkConnected&#34;</span><span class="p">:</span><span class="kc">true</span><span class="p">,</span>
    <span class="nt">&#34;status&#34;</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span>
    <span class="nt">&#34;QTime&#34;</span><span class="p">:</span><span class="mi">8</span><span class="p">,</span>
    <span class="nt">&#34;params&#34;</span><span class="p">:{</span>
      <span class="nt">&#34;q&#34;</span><span class="p">:</span><span class="s2">&#34;foundation&#34;</span><span class="p">}},</span>
  <span class="nt">&#34;response&#34;</span><span class="p">:{</span><span class="nt">&#34;numFound&#34;</span><span class="p">:</span><span class="mi">4</span><span class="p">,</span><span class="nt">&#34;start&#34;</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span><span class="nt">&#34;maxScore&#34;</span><span class="p">:</span><span class="mf">2.7879646</span><span class="p">,</span><span class="nt">&#34;docs&#34;</span><span class="p">:[</span>
      <span class="p">{</span>
        <span class="nt">&#34;id&#34;</span><span class="p">:</span><span class="s2">&#34;0553293354&#34;</span><span class="p">,</span>
        <span class="nt">&#34;cat&#34;</span><span class="p">:[</span><span class="s2">&#34;book&#34;</span><span class="p">],</span>
        <span class="nt">&#34;name&#34;</span><span class="p">:</span><span class="s2">&#34;Foundation&#34;</span><span class="p">,</span>
        <span class="nt">&#34;price&#34;</span><span class="p">:</span><span class="mf">7.99</span><span class="p">,</span>
        <span class="nt">&#34;price_c&#34;</span><span class="p">:</span><span class="s2">&#34;7.99,USD&#34;</span><span class="p">,</span>
        <span class="nt">&#34;inStock&#34;</span><span class="p">:</span><span class="kc">true</span><span class="p">,</span>
        <span class="nt">&#34;author&#34;</span><span class="p">:</span><span class="s2">&#34;Isaac Asimov&#34;</span><span class="p">,</span>
        <span class="nt">&#34;author_s&#34;</span><span class="p">:</span><span class="s2">&#34;Isaac Asimov&#34;</span><span class="p">,</span>
        <span class="nt">&#34;series_t&#34;</span><span class="p">:</span><span class="s2">&#34;Foundation Novels&#34;</span><span class="p">,</span>
        <span class="nt">&#34;sequence_i&#34;</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span>
        <span class="nt">&#34;genre_s&#34;</span><span class="p">:</span><span class="s2">&#34;scifi&#34;</span><span class="p">,</span>
        <span class="nt">&#34;_version_&#34;</span><span class="p">:</span><span class="mi">1574100232473411586</span><span class="p">,</span>
        <span class="nt">&#34;price_c____l_ns&#34;</span><span class="p">:</span><span class="mi">799</span><span class="p">}]</span>
<span class="p">}}</span>
</code></pre></td></tr></table>
</div>
</div><p>结果当中返回了文档的所有字段，如果只想返回<code>id</code>字段，需要指定<code>fl</code>参数：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">http://localhost:8983/solr/techproducts/select?q=foundation&amp;fl=id
</code></pre></td></tr></table>
</div>
</div><h3 id="字段查询">字段查询</h3>
<p>Solr的查询都是基于某些字段进行搜索的，上面默认是搜索的<code>_text_</code>字段，当然这个设置可以在配置集中修改。这里的<code>_text_</code>是一个copy field，包含的是其他字段的内容，如果只是想搜索某一个指定的字段，或者某几个指定的字段进行搜索，就需要指定字段名字了。假设需要搜索分类包含electronics的文档，参数<code>q</code>应该是<code>cat:electronics</code>，完整的url如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">http://localhost:8983/solr/techproducts/select?q=cat:electronics
</code></pre></td></tr></table>
</div>
</div><p>返回的数据如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-json" data-lang="json"><span class="p">{</span>
  <span class="nt">&#34;responseHeader&#34;</span><span class="p">:{</span>
    <span class="nt">&#34;zkConnected&#34;</span><span class="p">:</span><span class="kc">true</span><span class="p">,</span>
    <span class="nt">&#34;status&#34;</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span>
    <span class="nt">&#34;QTime&#34;</span><span class="p">:</span><span class="mi">6</span><span class="p">,</span>
    <span class="nt">&#34;params&#34;</span><span class="p">:{</span>
      <span class="nt">&#34;q&#34;</span><span class="p">:</span><span class="s2">&#34;cat:electronics&#34;</span><span class="p">}},</span>
  <span class="nt">&#34;response&#34;</span><span class="p">:{</span><span class="nt">&#34;numFound&#34;</span><span class="p">:</span><span class="mi">12</span><span class="p">,</span><span class="nt">&#34;start&#34;</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span><span class="nt">&#34;maxScore&#34;</span><span class="p">:</span><span class="mf">0.9614112</span><span class="p">,</span><span class="nt">&#34;docs&#34;</span><span class="p">:[</span>
      <span class="p">{</span>
        <span class="nt">&#34;id&#34;</span><span class="p">:</span><span class="s2">&#34;SP2514N&#34;</span><span class="p">,</span>
        <span class="nt">&#34;name&#34;</span><span class="p">:</span><span class="s2">&#34;Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133&#34;</span><span class="p">,</span>
        <span class="nt">&#34;manu&#34;</span><span class="p">:</span><span class="s2">&#34;Samsung Electronics Co. Ltd.&#34;</span><span class="p">,</span>
        <span class="nt">&#34;manu_id_s&#34;</span><span class="p">:</span><span class="s2">&#34;samsung&#34;</span><span class="p">,</span>
        <span class="nt">&#34;cat&#34;</span><span class="p">:[</span><span class="s2">&#34;electronics&#34;</span><span class="p">,</span>
          <span class="s2">&#34;hard drive&#34;</span><span class="p">],</span>
        <span class="nt">&#34;features&#34;</span><span class="p">:[</span><span class="s2">&#34;7200RPM, 8MB cache, IDE Ultra ATA-133&#34;</span><span class="p">,</span>
          <span class="s2">&#34;NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor&#34;</span><span class="p">],</span>
        <span class="nt">&#34;price&#34;</span><span class="p">:</span><span class="mf">92.0</span><span class="p">,</span>
        <span class="nt">&#34;price_c&#34;</span><span class="p">:</span><span class="s2">&#34;92.0,USD&#34;</span><span class="p">,</span>
        <span class="nt">&#34;popularity&#34;</span><span class="p">:</span><span class="mi">6</span><span class="p">,</span>
        <span class="nt">&#34;inStock&#34;</span><span class="p">:</span><span class="kc">true</span><span class="p">,</span>
        <span class="nt">&#34;manufacturedate_dt&#34;</span><span class="p">:</span><span class="s2">&#34;2006-02-13T15:26:37Z&#34;</span><span class="p">,</span>
        <span class="nt">&#34;store&#34;</span><span class="p">:</span><span class="s2">&#34;35.0752,-97.032&#34;</span><span class="p">,</span>
        <span class="nt">&#34;_version_&#34;</span><span class="p">:</span><span class="mi">1574100232511160320</span><span class="p">,</span>
        <span class="nt">&#34;price_c____l_ns&#34;</span><span class="p">:</span><span class="mi">9200</span><span class="p">}]</span>
     <span class="p">}}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="短语查询">短语查询</h3>
<p>短语查询可以理解为：多个词的精确匹配（顺序一致），比如要查询文档中包含&quot;multiple terms here&quot;短语，需要使用双引号包起来。例如查询<code>CAS latency</code>：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">http://localhost:8983/solr/techproducts/select?q=&#34;CAS+latency&#34;
</code></pre></td></tr></table>
</div>
</div><p>结果为：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">{
  &#34;responseHeader&#34;:{
    &#34;zkConnected&#34;:true,
    &#34;status&#34;:0,
    &#34;QTime&#34;:7,
    &#34;params&#34;:{
      &#34;q&#34;:&#34;\&#34;CAS latency\&#34;&#34;}},
  &#34;response&#34;:{&#34;numFound&#34;:2,&#34;start&#34;:0,&#34;maxScore&#34;:5.937691,&#34;docs&#34;:[
      {
        &#34;id&#34;:&#34;VDBDB1A16&#34;,
        &#34;name&#34;:&#34;A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM&#34;,
        &#34;manu&#34;:&#34;A-DATA Technology Inc.&#34;,
        &#34;manu_id_s&#34;:&#34;corsair&#34;,
        &#34;cat&#34;:[&#34;electronics&#34;,
          &#34;memory&#34;],
        &#34;features&#34;:[&#34;CAS latency 3,   2.7v&#34;],
        &#34;popularity&#34;:0,
        &#34;inStock&#34;:true,
        &#34;store&#34;:&#34;45.18414,-93.88141&#34;,
        &#34;manufacturedate_dt&#34;:&#34;2006-02-13T15:26:37Z&#34;,
        &#34;payloads&#34;:&#34;electronics|0.9 memory|0.1&#34;,
        &#34;_version_&#34;:1574100232590852096},
      {
        &#34;id&#34;:&#34;TWINX2048-3200PRO&#34;,
        &#34;name&#34;:&#34;CORSAIR  XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail&#34;,
        &#34;manu&#34;:&#34;Corsair Microsystems Inc.&#34;,
        &#34;manu_id_s&#34;:&#34;corsair&#34;,
        &#34;cat&#34;:[&#34;electronics&#34;,
          &#34;memory&#34;],
        &#34;features&#34;:[&#34;CAS latency 2,  2-3-3-6 timing, 2.75v, unbuffered, heat-spreader&#34;],
        &#34;price&#34;:185.0,
        &#34;price_c&#34;:&#34;185.00,USD&#34;,
        &#34;popularity&#34;:5,
        &#34;inStock&#34;:true,
        &#34;store&#34;:&#34;37.7752,-122.4232&#34;,
        &#34;manufacturedate_dt&#34;:&#34;2006-02-13T15:26:37Z&#34;,
        &#34;payloads&#34;:&#34;electronics|6.0 memory|3.0&#34;,
        &#34;_version_&#34;:1574100232584560640,
        &#34;price_c____l_ns&#34;:18500}]
  }}
</code></pre></td></tr></table>
</div>
</div><h3 id="组合搜索">组合搜索</h3>
<p>默认情况下，搜索多个词的时候，solr使用或的逻辑来查询，也就是说只要多个词中的一个匹配到了就是命中了。当需要组合逻辑搜索的时候可以使用<code>+</code>、<code>-</code>前缀，<code>+</code>表示必须匹配的词，<code>-</code>表示不能匹配的词。比如要查询文档中包含<code>electronics</code>和<code>music</code>，查询表达式为<code>+electronics +music</code>。solr的查询请求url需要进行编码，请求语句变成如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">http://localhost:8983/solr/techproducts/select?q=%2Belectronics%20%2Bmusic
</code></pre></td></tr></table>
</div>
</div><p>返回一个结果</p>
<p>如果要搜索文档中包含<code>electronics</code>，<em>但是</em>不包含<code>music</code>，查询表达式为<code>+electronics -music</code>。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-fallback" data-lang="fallback">http://localhost:8983/solr/techproducts/select?q=%2Belectronics+-music
</code></pre></td></tr></table>
</div>
</div><p>这个搜索会得到13个结果</p>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">Kevin Jiang</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2017-10-08
        
    </span>
  </p>
  
  <p class="copyright-item">
    <span class="item-title">许可协议</span>
    <span class="item-content"><a rel="license noopener" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank">CC BY-NC-ND 4.0</a></span>
  </p>
</div>
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/wechat_pay_1242x1242.jpg">
        <span>微信打赏</span>
      </label>
    <label class="qr-code-image" for="reward">
        <img class="image" src="/img/alipay_600x600.jpg">
        <span>支付宝打赏</span>
      </label>
  </div>
</div><footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/solr/">Solr</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/searchengine/solr7%E9%85%8D%E7%BD%AE%E5%90%8C%E4%B9%89%E8%AF%8D/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">solr7配置同义词</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/searchengine/%E8%87%AA%E5%AE%9A%E4%B9%89lucene%E7%9A%84%E5%88%86%E8%AF%8D%E5%99%A8analyzer/">
            <span class="next-text nav-default">自定义Lucene的分词器Analyzer</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:wenlin1988@126.com" class="iconfont icon-email" title="email"></a>
      <a href="https://www.linkedin.com/in/%E6%96%87%E6%9E%97-%E8%92%8B-0a3204126/" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="https://github.com/kevindragon" class="iconfont icon-github" title="github"></a>
  <a href="http://kevinjiang.info/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 -
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  

  <span class="copyright-year">
    &copy;
    2015 -
    2023<span class="heart"><i class="iconfont icon-heart"></i></span><span>Kevin Jiang</span>
    <a href="https://beian.miit.gov.cn/" target="_blank">湘ICP备2022022745号</a>
  </span>
</div>

    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.c99b103c33d1539acf3025e1913697534542c4a5aa5af0ccc20475ed2863603b.js"></script>
  <script type="text/javascript">
    window.MathJax = {
      tex: {
        inlineMath: [['$','$'], ['\\(','\\)']],
        tags: 'ams',
        }
    };
  </script>
  <script type="text/javascript" async src="/lib/mathjax/es5/tex-mml-chtml.js"></script>

<script id="baidu_analytics">
  var _hmt = _hmt || [];
  (function() {
    if (window.location.hostname === 'localhost') return;
    var hm = document.createElement("script"); hm.async = true;
    hm.src = "https://hm.baidu.com/hm.js?b73ff6d4afc4af9e582d8a5dc068bab9";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>






</body>
</html>
